﻿using HappyDining.HPDNServiceReference;
using HappyDining.MyUserControls;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Item Detail Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234232

namespace HappyDining.Views
{
    /// <summary>
    /// A page that displays details for a single item within a group while allowing gestures to
    /// flip through other items belonging to the same group.
    /// </summary>
    public sealed partial class ChiTietHoaDon : HappyDining.Common.LayoutAwarePage
    {
        private Table table = null;
        private string confirmPay = "Thanh toán";
        private string cancelPay = "Chưa đến lúc";

        public ChiTietHoaDon()
        {
            this.InitializeComponent();
        }

        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            // Allow saved page state to override the initial item to display
            if (pageState != null && pageState.ContainsKey("SelectedItem"))
            {
                navigationParameter = pageState["SelectedItem"];
            }

            table = (Table)navigationParameter;
            this.pageTitle.Text = table.TableName;

            showDanhSachChiTietHoaDon(table);

        }

        private async void showDanhSachChiTietHoaDon(Table table)
        {
            Service1Client client = new Service1Client();
            // Nếu bàn đang trống
            if (table.Status == 0)
            {
                MessageDialog md = new MessageDialog("Bàn hiện tại đang trống");
                md.Title = table.TableName.ToUpper();
                await md.ShowAsync();
            }

            // Bàn có thể đang có khách
            else
            {
                Bill bill = await client.GetTableCurrentBillAsync(table);
                // Chắc không xảy ra đâu, nhưng mà anh thích check thế đấy
                // Nó xảy ra trong trường hợp cài cắm CSDL bằng tay
                if (bill.Id == -1)
                {
                    MessageDialog md = new MessageDialog("Bàn hiện tại chưa gọi món");
                    md.Title = table.TableName.ToUpper();
                    await md.ShowAsync();
                }
                else
                {
                    this.chiTietStackPanel.Children.Clear();
                    List<BillDetails> bdList = await client.GetBillDetailsAsync(bill);
                    double total = 0;
                    foreach (BillDetails bd in bdList)
                    {
                        ChiTietHoaDonUserControl chiTietControl = new ChiTietHoaDonUserControl(bd);
                        this.chiTietStackPanel.Children.Add(chiTietControl);
                        total += bd.Price * bd.Count;
                    }
                    this.tongTien.Text = string.Format("{0:N0}", total) + " VND";
                }
            }
            
        }
        protected override void SaveState(Dictionary<String, Object> pageState)
        {
            //var selectedItem = this.flipView.SelectedItem;
        }

        

        private async void thanhToanButton_Tapped(object sender, TappedRoutedEventArgs e)
        {
            MessageDialog xacNhanThanhToanDialog
                = new MessageDialog("Xác nhận thanh toán?", "XÁC NHẬN");
            xacNhanThanhToanDialog.Commands.Add(new UICommand(
                    confirmPay,
                    new UICommandInvokedHandler(this.CommandInvokedHandler)
                ));
            xacNhanThanhToanDialog.Commands.Add(new UICommand(
                    cancelPay,
                    new UICommandInvokedHandler(this.CommandInvokedHandler)
                ));
            xacNhanThanhToanDialog.CancelCommandIndex = 1;
            await xacNhanThanhToanDialog.ShowAsync();
        }

        private async void CommandInvokedHandler(IUICommand command)
        {
            Debug.WriteLine("Đã chọn: " +command.Id+ " : " +command.Label);
            
            // Bấm nút hủy thanh toán
            if (command.Label == cancelPay)
            {
                return;
            }

            // Chắc chắn muốn thanh toán
            else
            {
                Service1Client client = new Service1Client();
                await client.TablePayAsync(this.table);
                this.Frame.Navigate(typeof(MainPage));
            }
        }
    }
}
